﻿using System;
using System.Collections.Generic;
using FishFillets.Data.Helpers;

namespace FishFillets.Data.levels
{
    public partial class start : BaseLevelScript
    {
        // ---------------------------------------------------------------
        // Init
        // ---------------------------------------------------------------
        public override void prog_init()
        {
            pokus = getRestartCount();
                        
            // -----------------------------------------------------------
            Func<Action> prog_init_room = (() =>
            {
                int pom1 = 0;
                int pom2 = 0;
                bool pomb1 = false;
                bool pomb2 = false;
                int boring = 0;

                room.uzreklnavod = 0;
                room.qcount = 0;

                if (pokus == 1)
                {
                    room.qnavod1 = 110;
                    room.qnavod2 = 500;
                }
                else
                {
                    room.qnavod1 = 400;
                    room.qnavod2 = 2000;
                }

                room.kecyoceli = random(100) + 80;
                room.malanepohne = 0;
                room.malanemuze = 0;
                room.tlustoch = 0;
                room.restrt = 0;

                return (() =>
                {
                    if (small.getAction() == "rest" && big.getAction() == "rest")
                    {
                        boring++;
                    }
                    else
                    {
                        boring = 0;
                    }

                    room.qcount++;
                    if (room.qcount == 3)
                    {
                        addm(15 + random(5), "1st-m-cotobylo");
                        addv(6, "1st-v-netusim");
                        addv(random(10) + 10, "1st-v-ven");
                        addm(3, "1st-m-pockej");
                    }

                    if (no_dialog() && trubka.cinnost == 0 && isReady(small) && isReady(big))
                    {
                        if (room.qnavod1 > 0 && boring >= room.qnavod1)
                        {
                            room.qnavod1 = -1;
                            addm(5, "1st-m-proc");
                            planBusy(small, true, 3);
                            addm(5, "1st-m-hej");
                        }
                        else if (game_getCycles() >= room.qnavod2 && room.qnavod2 != -1)
                        {
                            room.qnavod1 = -1;
                            planBusy(small, true, 3);
                            addm(5, "1st-m-hej");
                        }
                        if (room.qnavod1 == -1)
                        {
                            room.qnavod1 = 600;
                            room.qnavod2 = -1;
                            planBusy(big, true, 4);
                            addv(5, "1st-v-navod1");
                            planBusy(small, false, 3);
                            planBusy(big, false, 1);
                            //addm(20, "1st-m-navod2");
                            //addv(5, "1st-v-navod3");
                            planBusy(small, true, 100);
                            planBusy(big, true, 2);
                            addm(3, "1st-m-navod4");
                            addv(0, "1st-v-navod5");
                            addm(2, "1st-m-navod6");
                            planBusy(big, false, 3);
                            planBusy(small, false, 2);
                            addv(20, "1st-v-navod7");
                            addm(20, "1st-m-navod8");
                            if (room.uzreklnavod == 0)
                            {
                                room.uzreklnavod = 1;
                                addv(35, "1st-v-davej");
                                addm(0, "1st-m-nechtoho");
                                addv(5, "1st-v-takdobre");
                            }
                        }
                        if (room.kecyoceli == 0)
                        {
                            room.kecyoceli = random(600) + 300;
                            trubka.cinnost = 1;
                        }
                        else
                            room.kecyoceli--;

                        if (room.malanemuze == 0 && trubka.pohnul == 0 && small.X == 13)
                        {
                            room.malanemuze = 1;
                            addm(random(5) + 2, "1st-m-neprojedu");
                        }
                        if (room.malanepohne == 0 && trubka.pohnul == 0 && small.X == 12 && small.Y >= 10)
                        {
                            room.malanepohne = 1;
                            addm(0, "1st-m-nepohnu");
                        }
                        if (room.malanepohne > 0)
                        {
                            room.malanepohne++;
                            if (trubka.Direction == dir_left)
                            {
                                if (room.malanepohne < 50)
                                {
                                    addv(0,"1st-v-takukaz");
                                    room.malanepohne = -1;
                                }
                            }
                        }
                        if (room.malanepohne != -2 && trubka.Direction == dir_left)
                        {
                            addm(4, "1st-m-hmmm");
                            room.malanepohne = -2;
                        }
                        if (zidlev.X >= 20 && big.X >= 21 && big.Y == 15)
                        {
                            switch (room.tlustoch)
                            {
                                case 0:
                                    addv(10, "1st-v-nemuzu");
                                    if (random(100) < 50)
                                        addv(4, "1st-v-pribral");
                                    room.tlustoch++;
                                    break;
                                case 2:
                                    addv(10, "1st-v-posunout");
                                    room.tlustoch++;
                                    break;
                            }
                        }

                        if (big.X < 20 && room.tlustoch == 1)
                            room.tlustoch++;

                        if (room.restrt == 0 && trubka.X <= 9 && small.X <= trubka.X - 3)
                        {
                            planBusy(small, false);
                            addm(10, "1st-m-pokud");
                            planBusy(big, true, 3);
                            addv(3, "1st-v-znovu");
                            if (random(100) < 50)
                            {
                                addm(0, "1st-m-backspace");
                                addv(0, "1st-v-jedno");
                            }
                            planBusy(small, false, 3);
                            addv(5, "1st-v-najit");
                            planBusy(big, false);
                            room.restrt = 1;
                        }
                    }

                    if (no_dialog() && room.restrt == 0 && small.isOut() && isReady(big) &&
                       big.X <= 23 && zidlev.X >= 20)
                    {
                        addv(30, "1st-v-chyba");
                        planBusy(big, true);
                        addv(10, "1st-v-nedostanu");
                        planBusy(big, false);
                        planBusy(big, true, 50);
                        addv(3, "1st-v-stiskni");
                        if (random(100) < 50)
                        {
                            addm(0, "1st-m-backspace");
                            addv(0, "1st-v-jedno");
                        }
                        /* TODO: control panel
                        addv(5, "1st-v-najit");
                        */
                        planBusy(big, false);
                        room.restrt = 1;
                    }
                });
            });

            // -----------------------------------------------------------
            Func<Action> prog_init_trubka = (() =>
            {
                int pom1 = 0;
                int pom2 = 0;
                bool pomb1 = false;
                bool pomb2 = false;

                trubka.cinnost = 0;
                trubka.pohnul = 0;

                return (() =>
                {
                    if (trubka.Direction == Model.GameItems.FishDirection.Left || trubka.Direction == Model.GameItems.FishDirection.Right)
                    {
                        trubka.pohnul = 1;
                    }
                    Dictionary<int, Action> anim_table = new Dictionary<int, Action>();
                    anim_table[1] = (() =>
                    {
                        trubka.cinnost = trubka.cinnost + 1;
                        trubka.afaze = trubka.afaze + 1;
                        trubka.delay = 7;
                    });
                    anim_table[5] = (() =>
                    {
                        if (trubka.delay > 0)
                            trubka.delay = trubka.delay - 1;
                        else
                            trubka.cinnost = trubka.cinnost + 1;
                    });
                    anim_table[6] = (() =>
                    {
                        trubka.afaze = 5;
                        trubka.cinnost = trubka.cinnost + 1;
                        trubka.delay = 8;
                    });
                    anim_table[7] = (() =>
                    {
                        if (trubka.delay > 0)
                        {
                            if (random(100) < 10)
                                trubka.afaze = 4;
                            else
                                trubka.afaze = 5;
                            trubka.delay = trubka.delay - 1;
                        }
                        else
                            trubka.cinnost = trubka.cinnost + 1;
                    });
                    anim_table[8] = (() =>
                    {
                        trubka.planDialog(0, "1st-x-ocel");
                        trubka.cinnost = trubka.cinnost + 1;
                    });
                    anim_table[9] = (() =>
                    {
                        if (trubka.isTalking())
                        {
                            if (trubka.afaze == 4)
                                trubka.afaze = 5;
                            if (math.mod(game_getCycles(), 3) == 1)
                            {
                                if (random(2) == 0)
                                    trubka.afaze = trubka.afaze + 1;
                                else
                                    trubka.afaze = trubka.afaze - 1;
                                if (trubka.afaze == 4)
                                    trubka.afaze = 8;
                                else if (trubka.afaze == 9)
                                    trubka.afaze = 5;
                            }
                            if (trubka.afaze == 5 && random(100) < 40)
                                trubka.afaze = 4;
                        }
                        else
                        {
                            trubka.cinnost = trubka.cinnost + 1;
                        }
                    });
                    anim_table[10] = (() =>
                    {
                        trubka.afaze = 5;
                        trubka.cinnost = trubka.cinnost + 1;
                    });
                    anim_table[11] = (() =>
                    {
                        trubka.afaze = trubka.afaze - 1;
                        trubka.cinnost = trubka.cinnost + 1;
                    });
                    anim_table[16] = (() =>
                    {
                        trubka.cinnost = 0;
                    });

                    anim_table[2] = anim_table[1];
                    anim_table[3] = anim_table[1];
                    anim_table[4] = anim_table[1];
                    anim_table[12] = anim_table[11];
                    anim_table[13] = anim_table[11];
                    anim_table[14] = anim_table[11];
                    anim_table[15] = anim_table[11];
                    Switch(trubka.cinnost, anim_table);
                    trubka.updateAnim();
                });
            });

            // --------------------
            update_table = new List<Action> { prog_init_room(), prog_init_trubka() };
        }

        private void Switch(int key, Dictionary<int, Action> animTable)
        {
            if (animTable.ContainsKey(key))
            {
                animTable[key]();
            }
        }
    }
}
